package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/16 10:37
 * @description
 */
public class Code99 {
    private List<Integer> list = new ArrayList<>();
    private int index = 0;

    public void recoverTree(TreeNode root) {
        setList(root);
        Collections.sort(list);
        setNode(root);
    }

    private void setList(TreeNode node) {
        if (node != null) {
            list.add(node.val);
            setList(node.left);
            setList(node.right);
        }
    }

    private void setNode(TreeNode node) {
        if (node != null) {
            setNode(node.left);
            node.val = list.get(index++);
            setNode(node.right);
        }
    }
}
